package com.example.tys.baseapplication.View.recycleView.swiped2.core;

import android.graphics.Canvas;
import android.support.v7.widget.RecyclerView;

import com.example.tys.baseapplication.View.recycleView.swiped2.BaseSwipeViewHolder;


/**
 * @author tys
 */
public class ItemTouchHelperSwipeCallback extends ItemTouchHelper.SimpleCallback {

    public ItemTouchHelperSwipeCallback(int swipeDirs) {
        super(0, swipeDirs);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return false;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        if (viewHolder instanceof BaseSwipeViewHolder) {
            BaseSwipeViewHolder holder = (BaseSwipeViewHolder) viewHolder;
            if (dX < 0) {
                if (dX < -holder.getRightContainerView().getWidth()) {
                    dX = -holder.getRightContainerView().getWidth();
                }
            } else {
                if (dX > holder.getLeftContainerView().getWidth()) {
                    dX = holder.getLeftContainerView().getWidth();
                }
            }
            holder.getContentView().setTranslationX(dX);
        } else {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
    }
}
